﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_HRM_PR_ExtraTypes : IDAL_HRM_PR_ExtraTypes
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();
        

        public D_HRM_PR_ExtraTypes()
        {

        }

        public IList<M_HRM_PR_ExtraTypes> GetFull(Nullable<int> emp_type_seq
            , string ext_flag, string ext_tax, string ext_monthly)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_HRM_PR_ExtraTypes> infoList = new List<M_HRM_PR_ExtraTypes>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@emp_type_seq", emp_type_seq);
                dbManager.AddParameters(2, "@ext_flag", ext_flag);
                dbManager.AddParameters(3, "@ext_tax", ext_tax);
                dbManager.AddParameters(4, "@ext_monthly", ext_monthly);

                strSQL = "SELECT ext.ext_seq, ext.emp_type_seq"
                    + ", ext.ext_code, ext.ext_name, ext.ext_name_short"
                    + ", ext.cmd_type_seq, ext.ext_flag, ext.ext_tax, ext.ext_monthly"
                    + ", ext.remarks, ext.ext_oth_code, ext.edit_system, ext.contact_seq"

                    + ", c.contact_name"
                    + ", concat(ext.ext_code, ' - ', ext.ext_name) as ext_code_name"
                    + ", case when ext.ext_flag = 'I' then 'เงินได้' else 'เงินหัก' end as ext_flag_name"
                    + ", case when ext.ext_tax = 'Y' then 'คิดภาษี' else 'ไม่คิดภาษี' end as ext_tax_name"
                    + ", case when ext.ext_monthly = 'Y' then 'ทุกเดือน' else 'ไม่ทุกเดือน' end as ext_monthly_name"

                    + ", ext.record_status, ext.created_by, ext.created_when, ext.updated_by, ext.updated_when"
                    + " FROM hrm_pr_extra_types ext"
                    + " LEFT JOIN fim_mas_contacts c ON ext.contact_seq = c.contact_seq"
                    + " WHERE ext.record_status not like @delete_status"
                    + (emp_type_seq != null ? " AND ext.emp_type_seq = @emp_type_seq" : "")
                    + (ext_flag != null ? " AND ext.ext_flag = @ext_flag" : "")
                    + (ext_tax != null ? " AND ext.ext_tax = @ext_tax" : "")
                    + (ext_monthly != null ? " AND ext.ext_monthly = @ext_monthly" : "");

                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_HRM_PR_ExtraTypes info = new M_HRM_PR_ExtraTypes();
                        info.ext_seq = Convert.ToInt32(drc[i]["ext_seq"]);
                        info.emp_type_seq = cn.ConvertNullInt(drc[i]["emp_type_seq"]);
                        info.ext_code = drc[i]["ext_code"].ToString();
                        info.ext_name = drc[i]["ext_name"].ToString();
                        info.ext_name_short = drc[i]["ext_name_short"].ToString();
                        info.cmd_type_seq = cn.ConvertNullInt(drc[i]["cmd_type_seq"]);
                        info.ext_flag = drc[i]["ext_flag"].ToString();
                        info.ext_tax = drc[i]["ext_tax"].ToString();
                        info.ext_monthly = drc[i]["ext_monthly"].ToString();
                        info.remarks = drc[i]["remarks"].ToString();
                        info.ext_oth_code = drc[i]["ext_oth_code"].ToString();
                        info.edit_system = drc[i]["edit_system"].ToString();
                        info.contact_seq = cn.ConvertNullInt(drc[i]["contact_seq"]);

                        info.contact_name = drc[i]["contact_name"].ToString();
                        info.ext_code_name = drc[i]["ext_code_name"].ToString();
                        info.ext_flag_name = drc[i]["ext_flag_name"].ToString();
                        info.ext_tax_name = drc[i]["ext_tax_name"].ToString();
                        info.ext_monthly_name = drc[i]["ext_monthly_name"].ToString();

                        info.record_status = drc[i]["record_status"].ToString();
                        info.created_by = cn.ConvertNullInt(drc[i]["created_by"]);
                        info.created_when = cn.ConvertNullDateTime(drc[i]["created_when"]);
                        info.updated_by = cn.ConvertNullInt(drc[i]["updated_by"]);
                        info.updated_when = cn.ConvertNullDateTime(drc[i]["updated_when"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_HRM_PR_ExtraTypes> GetList(Nullable<int> emp_type_seq
            , string ext_flag, string ext_tax, string ext_monthly)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_HRM_PR_ExtraTypes> infoList = new List<M_HRM_PR_ExtraTypes>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@emp_type_seq", emp_type_seq);
                dbManager.AddParameters(2, "@ext_flag", ext_flag);
                dbManager.AddParameters(3, "@ext_tax", ext_tax);
                dbManager.AddParameters(4, "@ext_monthly", ext_monthly);

                strSQL = "SELECT ext.ext_seq, ext.ext_name"
                    + " FROM hrm_pr_extra_types ext"
                    + " WHERE ext.record_status not like @delete_status"
                    + (emp_type_seq != null ? " AND ext.emp_type_seq = @emp_type_seq" : "")
                    + (ext_flag != null ? " AND ext.ext_flag = @ext_flag" : "")
                    + (ext_tax != null ? " AND ext.ext_tax = @ext_tax" : "")
                    + (ext_monthly != null ? " AND ext.ext_monthly = @ext_monthly" : "");

                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_HRM_PR_ExtraTypes info = new M_HRM_PR_ExtraTypes();
                        info.ext_seq = Convert.ToInt32(drc[i]["ext_seq"]);
                        info.ext_name = drc[i]["ext_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_HRM_PR_ExtraTypes> GetMoneyReqIncomeList(Nullable<int> pay_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_HRM_PR_ExtraTypes> infoList = new List<M_HRM_PR_ExtraTypes>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@pay_seq", pay_seq);

                strSQL = "SELECT prd.ext_seq, ext.ext_name"
                    + " FROM hrm_pr_payroll_dtls prd"
                    + " LEFT JOIN hrm_pr_extra_types ext ON prd.ext_seq = ext.ext_seq"
                    + " WHERE prd.record_status not like @delete_status"
                    + " AND ext.ext_flag = 'I'"
                    + (pay_seq != null ? " and prd.pay_seq = @pay_seq" : "")
                    + " group by prd.ext_seq, ext.ext_name";
                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_HRM_PR_ExtraTypes info = new M_HRM_PR_ExtraTypes();
                        info.ext_seq = Convert.ToInt32(drc[i]["ext_seq"]);
                        info.ext_name = drc[i]["ext_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_HRM_PR_ExtraTypes GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_HRM_PR_ExtraTypes info = new M_HRM_PR_ExtraTypes();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT ext.ext_seq, ext.emp_type_seq"
                    + ", ext.ext_code, ext.ext_name, ext.ext_name_short"
                    + ", ext.cmd_type_seq, ext.ext_flag, ext.ext_tax, ext.ext_monthly"
                    + ", ext.remarks, ext.ext_oth_code, ext.edit_system"

                    + ", concat(ext.ext_code, ' - ', ext.ext_name) as ext_code_name"
                    + ", case when ext.ext_flag = 'I' then 'เงินได้' else 'เงินหัก' end as ext_flag_name"
                    + ", case when ext.ext_tax = 'Y' then 'คิดภาษี' else 'ไม่คิดภาษี' end as ext_tax_name"
                    + ", case when ext.ext_monthly = 'Y' then 'ทุกเดือน' else 'ไม่ทุกเดือน' end as ext_monthly_name"

                    + ", ext.record_status, ext.created_by, ext.created_when, ext.updated_by, ext.updated_when"
                    + " FROM hrm_pr_extra_types ext"
                    + " where ext.ext_seq = @seq";
                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.ext_seq = Convert.ToInt32(drc[0]["ext_seq"]);
                    info.emp_type_seq = cn.ConvertNullInt(drc[0]["emp_type_seq"]);
                    info.ext_code = drc[0]["ext_code"].ToString();
                    info.ext_name = drc[0]["ext_name"].ToString();
                    info.ext_name_short = drc[0]["ext_name_short"].ToString();
                    info.cmd_type_seq = cn.ConvertNullInt(drc[0]["cmd_type_seq"]);
                    info.ext_flag = drc[0]["ext_flag"].ToString();
                    info.ext_tax = drc[0]["ext_tax"].ToString();
                    info.ext_monthly = drc[0]["ext_monthly"].ToString();
                    info.remarks = drc[0]["remarks"].ToString();
                    info.ext_oth_code = drc[0]["ext_oth_code"].ToString();
                    info.edit_system = drc[0]["edit_system"].ToString();
                    info.contact_seq = cn.ConvertNullInt(drc[0]["contact_seq"]);

                    info.contact_name = drc[0]["contact_name"].ToString();
                    info.ext_code_name = drc[0]["ext_code_name"].ToString();
                    info.ext_flag_name = drc[0]["ext_flag_name"].ToString();
                    info.ext_tax_name = drc[0]["ext_tax_name"].ToString();
                    info.ext_monthly_name = drc[0]["ext_monthly_name"].ToString();

                    info.record_status = drc[0]["record_status"].ToString();
                    info.created_by = cn.ConvertNullInt(drc[0]["created_by"]);
                    info.created_when = cn.ConvertNullDateTime(drc[0]["created_when"]);
                    info.updated_by = cn.ConvertNullInt(drc[0]["updated_by"]);
                    info.updated_when = cn.ConvertNullDateTime(drc[0]["updated_when"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Insert(M_HRM_PR_ExtraTypes info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(16);

                dbManager.AddParameters(0, "@ext_seq", info.ext_seq);
                dbManager.AddParameters(1, "@emp_type_seq", info.emp_type_seq);
                dbManager.AddParameters(2, "@ext_code", info.ext_code);
                dbManager.AddParameters(3, "@ext_name", info.ext_name);
                dbManager.AddParameters(4, "@ext_name_short", info.ext_name_short);
                dbManager.AddParameters(5, "@cmd_type_seq", info.cmd_type_seq);
                dbManager.AddParameters(6, "@ext_flag", info.ext_flag);
                dbManager.AddParameters(7, "@ext_tax", info.ext_tax);
                dbManager.AddParameters(8, "@ext_monthly", info.ext_monthly);
                dbManager.AddParameters(9, "@remarks", info.remarks);
                dbManager.AddParameters(10, "@ext_oth_code", info.ext_oth_code);
                dbManager.AddParameters(11, "@edit_system", info.edit_system);
                dbManager.AddParameters(12, "@contact_seq", info.contact_seq);

                dbManager.AddParameters(13, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(14, "@created_by", info.created_by);
                dbManager.AddParameters(15, "@created_when", info.created_when);

                strSQL = "INSERT INTO hrm_pr_extra_types"
                    + "(emp_type_seq, ext_code, ext_name, ext_name_short, cmd_type_seq"
                    + ", ext_flag, ext_tax, ext_monthly, remarks, ext_oth_code, edit_system, contact_seq"
                    + ", record_status, created_by, created_when)"
                    + " VALUES(@emp_type_seq"
                        + ", @ext_code"
                        + ", @ext_name"
                        + ", @ext_name_short"
                        + ", @cmd_type_seq"
                        + ", @ext_flag"
                        + ", @ext_tax"
                        + ", @ext_monthly"
                        + ", @remarks"
                        + ", @ext_oth_code"
                        + ", @edit_system"
                        + ", @contact_seq"

                        + ", @record_status"
                        + ", @created_by"
                        + ", @created_when)";
                

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(M_HRM_PR_ExtraTypes info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(14);

                dbManager.AddParameters(0, "@ext_seq", info.ext_seq);
                dbManager.AddParameters(1, "@emp_type_seq", info.emp_type_seq);
                dbManager.AddParameters(2, "@ext_code", info.ext_code);
                dbManager.AddParameters(3, "@ext_name", info.ext_name);
                dbManager.AddParameters(4, "@ext_name_short", info.ext_name_short);
                dbManager.AddParameters(5, "@cmd_type_seq", info.cmd_type_seq);
                dbManager.AddParameters(6, "@ext_flag", info.ext_flag);
                dbManager.AddParameters(7, "@ext_tax", info.ext_tax);
                dbManager.AddParameters(8, "@ext_monthly", info.ext_monthly);
                dbManager.AddParameters(9, "@remarks", info.remarks);
                dbManager.AddParameters(10, "@ext_oth_code", info.ext_oth_code);
                dbManager.AddParameters(11, "@contact_seq", info.contact_seq);

                dbManager.AddParameters(12, "@updated_by", info.updated_by);
                dbManager.AddParameters(13, "@updated_when", info.updated_when);

                strSQL = "UPDATE hrm_pr_extra_types"
                    + " set emp_type_seq = @emp_type_seq"
                    + ", ext_code = @ext_code"
                    + ", ext_name = @ext_name"
                    + ", ext_name_short = @ext_name_short"
                    + ", cmd_type_seq = @cmd_type_seq"
                    + ", ext_flag = @ext_flag"
                    + ", ext_tax = @ext_tax"
                    + ", ext_monthly = @ext_monthly"
                    + ", remarks = @remarks"
                    + ", ext_oth_code = @ext_oth_code"
                    + ", contact_seq = @contact_seq"

                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE ext_seq = @ext_seq";
                

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(M_HRM_PR_ExtraTypes info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@ext_seq", info.ext_seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.updated_by);
                dbManager.AddParameters(3, "@updated_when", info.updated_when);

                strSQL = "UPDATE hrm_pr_extra_types"
                    + " SET record_status = @record_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE ext_seq = @ext_seq";
                

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
